/*
# ------------------------------------------------------------------------
# JD Base Theme
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
 */
(function(jQuery) {
	jQuery.fn.JDMegaMenu = function(vars) {
		vars = vars != undefined ? vars : {};
		var durationIn = vars.durationIn == undefined ? 300 : vars.durationIn;
		var durationOut = vars.durationOut == undefined ? 400
				: vars.durationOut;
		var effectIn = vars.effectIn == undefined ? 'jswing' : vars.effectIn;
		var effectOut = vars.effectOut == undefined ? 'jswing' : vars.effectOut;
		var delay = vars.delay == undefined ? 1000 : vars.delay;
		var direction = vars.direction == undefined ? 0 : vars.direction;
		var wdWidth = jQuery(window).width();
		/* Fix getting browser window width for IE */
		var IE = jQuery.browser.msie;
		if (IE) {
			if (wdWidth < jQuery(document).width())
				wdWidth = jQuery(document).width();
		}
		var timeout = null;
		var currentId = null;
		var rtl = false;
		var hideside = direction ? 'right' : 'left';
		var marginside1 = direction ? 'margin-right' : 'margin-left';

		/* Set menu in the default style */
		jQuery('.childcontent').each(
				function() {
					var height = jQuery(this).height();
					jQuery('.childcontent-inner-wrap', this).css('top',
							'-' + height + 'px');
				});

		var overflow = function(id, hidden) {
			if (hidden) {
				jQuery('.childcontent-' + id).css('overflow', 'hidden');
			} else
				jQuery('.childcontent-' + id).css('overflow', '');
		}
		/* Check and Set rtl mode */
		var checkRTL = function(sub) {
			var docWidth = jQuery(document).width();
			if (docWidth > wdWidth) {
				rtl = true;
				if (sub)
					wdWidth = docWidth;
			} else
				rtl = false;
		}
		// console.log('donglt');
		/* Show a level 0 menu */
		var slidedown = function() {
			var childcontent = jQuery('.childcontent-' + currentId);
			/* Reset rtl mode */
			if (childcontent.hasClass('rtl')) {
				rtl = true;
			} else {
				if (childcontent.hasClass('ltr'))
					rtl = false;
				else {
					childcontent.css('right', 'auto');
					checkRTL(0);
				}
			}

			if (rtl) {
				var width = childcontent.width();
				var m_width = jQuery('.main').width();
				var left = (m_width - width) / 2;
				childcontent.css(hideside, left + 'px');
				childcontent.addClass('rtl');
			} else {
				childcontent.addClass('ltr');
				childcontent.css(hideside, 'auto');
			}
			if (direction) { // fix for Chrome, Opera
				childcontent.css('visibility', 'visible');
				childcontent.css('position', 'absolute');
				if (IE) {// Fix IE
					childcontent.css(hideside, '0');
					childcontent.parent().css('position', 'relative');
				}
			}
			jQuery('.childcontent-inner-wrap-' + currentId).stop(true, false)
					.animate( {
						'top' : 0
					}, durationIn, effectIn, function() {
						overflow(currentId, 0);
					});
		}
		/* Hide a level 0 menu */
		var slideup = function() {
			/* Backup currentId */
			var id = currentId;
			overflow(id, 1);
			var height = jQuery('.childcontent-' + currentId).height();
			jQuery('.childcontent-inner-wrap-' + currentId)
					.stop(true, false)
					.animate(
							{
								'top' : '-' + height + 'px'
							},
							durationOut,
							effectOut,
							function() {
								jQuery('.childcontent-' + id).css(hideside, '');
								if (direction) {// fix for Chrome, Opera
									jQuery('.childcontent-' + id).css(
											'visibility', 'hidden');
									jQuery('.childcontent-' + id).css(
											'position', 'fixed');
								}
							});
		}

		/* Show a sub menu */
		var showChild = function(id) {
			var childcontent = jQuery('.childcontent-' + id);
			var childcontentinnerwrap = jQuery('.childcontent-inner-wrap-' + id);
			var t = jQuery('#' + id);
			var width = childcontentinnerwrap.width();
			var cur_rtl = rtl;
			var pr_width = jQuery('#' + id).parent().parent().width();

			var l_width = width;
			/*
			 * Check available expand direction of element is right to left or
			 * reverse
			 */
			if (t.hasClass('rtl')) {
				childcontent.css(marginside1, '-' + l_width + 'px');
				childcontent.css('top', '');
				childcontent.css(hideside, 'auto');
			}
			if (t.hasClass('ltr')) {
				childcontent.css(hideside, 'auto');
			}
			if (!t.hasClass('rtl') && !t.hasClass('ltr')) {
				/* Expand in hidden to get temporary width of document */

				childcontent.css(hideside, 'auto');
				/*
				 * Compare the width of window and document to decide the
				 * direction
				 */
				checkRTL(1);
				/* Setup position */
				if (rtl) {
					childcontent.css(marginside1, '-' + l_width + 'px');
					childcontent.css('top', '');
					childcontentinnerwrap.css(marginside1, width);
					childcontent.css(hideside, 'auto');
					/* Add a class for caching */
					t.addClass('rtl');
				} else {
					childcontent.css(hideside, 'auto');
					/* Add a class for caching */
					t.addClass('ltr');
				}
			}
			childcontentinnerwrap.css('top', '');
			/* Add a class for menu status */
			t.addClass('showing');
			/* Begin animating */
			if (direction) {
				childcontent.css('visibility', 'visible');
				childcontent.css('position', 'absolute');
				childcontentinnerwrap.stop(true, false).animate( {
					'marginRight' : '0px'
				}, durationIn, effectIn, function() {
					overflow(id, 0);
				});
			} else {
				childcontentinnerwrap.stop(true, false).animate( {
					'marginLeft' : '0px'
				}, durationIn, effectIn, function() {
					overflow(id, 0);
				});
			}
		}
		/* Hide a sub menu */
		var hideChild = function(id) {
			var childcontent = jQuery('.childcontent-' + id);
			var childcontentinnerwrap = jQuery('.childcontent-inner-wrap-' + id);
			var t = jQuery('#' + id);
			var height = childcontent.height();
			var width = childcontentinnerwrap.css('width');
			overflow(id, 1);
			/* Remove status */
			t.removeClass('showing');
			if (t.hasClass('rtl')) {
				/* Stop current animation and Begin hiding animation */
				if (direction) {
					childcontentinnerwrap.stop(true, false).animate( {
						'marginRight' : width
					}, durationOut, effectOut, function() {
						childcontent.css(hideside, '');
						childcontentinnerwrap.css('top', '-' + height + 'px');
						childcontent.css('visibility', 'hidden');
						childcontent.css('position', 'fixed');
					});
				} else {
					childcontentinnerwrap.stop(true, false).animate( {
						'marginLeft' : width
					}, durationOut, effectOut, function() {
						childcontent.css(hideside, '');
						childcontentinnerwrap.css('top', '-' + height + 'px');
					});
				}
			} else {
				/* Stop current animation and Begin hiding animation */
				if (direction) {
					childcontentinnerwrap.stop(true, false).animate( {
						'marginRight' : '-' + width
					}, durationOut, effectOut, function() {
						childcontent.css(hideside, '');
						childcontentinnerwrap.css('top', '-' + height + 'px');
						childcontent.css('visibility', 'hidden');
						childcontent.css('position', 'fixed');
					});
				} else {
					childcontentinnerwrap.stop(true, false).animate( {
						'marginLeft' : '-' + width
					}, durationOut, effectOut, function() {
						childcontent.css(hideside, '');
						childcontentinnerwrap.css('top', '-' + height + 'px');
					});
				}
			}

		}
		/* Collect menu items */
		jQuery('.ja-megamenu  li').each(
				function() {
					var id = jQuery(this).attr('id');
					/* Set sub-menu style in default LTR */
					if (!jQuery(this).parent().hasClass('level0')) {
						var width = jQuery('.childcontent-inner-wrap-' + id)
								.css('width');
						jQuery('.childcontent-inner-wrap-' + id).css(
								marginside1, '-' + width);
					}
					/* Bind the mouseover event to each menu item */
					jQuery(this).bind(
							'mouseenter',
							function(event) {
								if (!jQuery(this).hasClass('over')
										&& !jQuery(this).hasClass('group')) {
									jQuery(this).addClass('over');
									jQuery('a', this).eq(0).addClass('over');
								}
								clearTimeout(timeout);
								timeout = null;
								/* Mouseover a level 0 menu item */
								if (jQuery(this).parent().hasClass('level0')) {
									if (currentId && id != currentId) {
										slideup();
									}
									if (jQuery(this).hasClass('haschild')) {
										currentId = id;
										slidedown();
									}
								}
								/* Mouseover a sub-menu item */
								else {
									if (jQuery(this).hasClass('haschild')
											&& !jQuery(this)
													.hasClass('showing')) {
										showChild(id);
									}
								}
							});
					/* Bind the mouseleave event to sub-menu items */
					jQuery(this).bind('mouseleave', function() {
						jQuery(this).removeClass('over');
						jQuery('a', this).eq(0).removeClass('over');
						if (!jQuery(this).parent().hasClass('level0')) {
							if (jQuery(this).hasClass('haschild')) {
								hideChild(id);
							}
						}
					});
				});
		/* Bind the mouseout event to menu */
		jQuery('.ja-megamenu').bind('mouseleave', function() {
			if (currentId) {
				/* delay to hide menu item */
				timeout = setTimeout(slideup, delay);
			}
		});
		/* Fix cursor for IE6 & IE7 */
		jQuery('.ja-megamenu span').css('cursor', 'pointer');
	}

})(jQuery);